
$(function(){

    /*宫格抽奖*/
    var luck={
        index:-1,	//当前转动到哪个位置，起点位置
        count:0,	//总共有多少个位置
        timer:0,	//setTimeout的ID，用clearTimeout清除
        speed:20,	//初始转动速度
        times:0,	//转动次数
        cycle:50,	//转动基本次数：即至少需要转动多少次再进入抽奖环节
        prize:-1,	//中奖位置
        init:function(id){
            if ($("#"+id).find(".luck-unit").length>0) {
                $luck = $("#"+id);
                $units = $luck.find(".luck-unit");
                this.obj = $luck;
                this.count = $units.length;
                $luck.find(".luck-unit-"+this.index).addClass("active");
            };
        },


        roll:function(){
            var index = this.index;
            var count = this.count;
            var luck = this.obj;
            $(luck).find(".luck-unit-"+index).removeClass("active");
            index += 1;
            if (index>count-1) {
                index = 0;
            };
            $(luck).find(".luck-unit-"+index).addClass("active");
            this.index=index;
            return false;
        },
        stop:function(index){
            this.prize=index;
            return false;
        }
    };


    function roll(){
        luck.times += 1;
        luck.roll();
        if (luck.times > luck.cycle+10 && luck.prize==luck.index) {
            clearTimeout(luck.timer);
            /*判断中奖弹窗*/
            switch (luck.prize){
                case 0:
                    $('#ipx').fadeIn();
                    break;
                case 1:
                    $('#money100').fadeIn();
                    break;
                case 2:
                    $('#money500').fadeIn();
                    break;
                case 3:
                    $('#money100_2').fadeIn();
                    break;
                case 4:
                    $('#money500_2').fadeIn();
                    break;
                case 5:
                    $('#none').fadeIn();
                    break;
                case 6:
                    $('#aiqiyi').fadeIn();
                    break;
                case 7:
                    $('#glod100').fadeIn();
                    break;
            }
            luck.prize=-1;
            luck.times=0;
            click=false;
        }else{
            if (luck.times<luck.cycle) {
                luck.speed -= 10;
            }else if(luck.times==luck.cycle) {
                var index = Math.random()*(luck.count)|0;
                luck.prize = index;
            }else{
                if (luck.times > luck.cycle+10 && ((luck.prize==0 && luck.index==7) || luck.prize==luck.index+1)) {
                    luck.speed += 110;
                }else{
                    luck.speed += 20;
                }
            }
            if (luck.speed<40) {
                luck.speed=40;
            };

            luck.timer = setTimeout(roll,luck.speed);
        }
        return false;
    }






    /*倒计时*/
    var predeterminedTime = '2019/02/29 00:00:00';
    (function leftTimer(pdate){
        var leftTime = (new Date(pdate)) - (new Date()); //计算剩余的毫秒数
        var days = parseInt(leftTime / 1000 / 60 / 60 / 24 , 10); //计算剩余的天数
        var hours = parseInt(leftTime / 1000 / 60 / 60 % 24 , 10); //计算剩余的小时
        var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟
        var seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数
        days = checkTime(days);
        hours = checkTime(hours);
        minutes = checkTime(minutes);
        seconds = checkTime(seconds);
        setTimeout(function(){
            leftTimer(predeterminedTime)
        },1000);
        $('#day').text(days);
        $('#hours').text(hours);
        $('#minutes').text(minutes);
        $('#seconds').text(seconds);
    })(predeterminedTime);

    //将0-9的数字前面加上0，例1变为01
    function checkTime(i){
        if(i<10)
        {
            i = "0" + i;
        }
        return i;
    }




    /*抽奖*/
    luck.init('luck');
    var choujiangcishu = 2;
    var click=false;

    function choujiang(){
        choujiangcishu--;
        if(choujiangcishu<0){
            $('#cishuyongwan').fadeIn();
            return;
        }

        $('#choujiangcishu').text('抽奖次数：'+choujiangcishu+'次');
        luck.roll();
        if(click) {
            return false;
        }
        else{
            luck.speed=100;
            roll();
            click=true;
            return false;
        }
    }
    /*点击抽奖*/
    $('.luck5').click(function(){
        choujiang();
    });



    /*查看奖品页面*/
    $('.seeGift').click(function(){
        var giftId = $(this).data('id');//取出弹窗id
        $('.tanchuang').hide();//关闭弹窗
        $('#'+giftId).fadeIn();//把对应的奖品弹窗显示出来
    });

    /*关闭弹窗，icon点击事件*/
    $('.tanchuang i').click(function(){
        $('.tanchuang').hide();//关闭弹窗
    });

    /*再抽一次*/
    $('.again').click(function(){
        $('.tanchuang').hide();//关闭弹窗
       if(choujiangcishu<=0) {
           $('#cishuyongwan').fadeIn();
       }else{
           choujiang();
       }
    });

    /*输入信息领取iphonx*/



    $('#lingqu').click(function(){

        var name = $('#Name').val();
        var phone = $('#Phone').val();
        var agg = $('#Agg').val();

        if( name==="" || phone==="" || agg==="" ){
            alert('请输入完整信息');
            return;
        }

        var xingming = /^[\u4e00-\u9fa5]{0,}$/
        var dianhua = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/

        if(name.length<8 && xingming.test(name)){

            if(phone.length=11 && dianhua.test(phone)){

                if(agg.length>6){

                    $('#Name').val('');
                    $('#Phone').val('');
                    $('#Agg').val('');

                    $('.tanchuang').hide();//关闭弹窗

                }else{
                    alert('请输入正确地址');
                }


            }else{
                alert('请输入正确手机号');
            }

        }else{
            alert('请正确输入信息');
        }



    });


    /*活动说明*/

    $('#sure').click(function(){

        $('#gift8').fadeIn();

    });

    /*关闭活动说明弹窗*/

    $('#know').click(function(){
        $('.tanchuang').hide();//关闭弹窗
    });



});










